package com.aries.crossgate.modules.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.aries.crossgate.common.log.config.Log;
import com.aries.crossgate.common.utils.Constant;
import com.aries.crossgate.common.utils.JsonDataFormate;
import com.aries.crossgate.common.utils.R;
import com.aries.crossgate.modules.sys.entity.SysUserOnline;
import com.aries.crossgate.modules.sys.service.SysOnlineService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Collection;
import java.util.List;

/**
 * java类简单作用描述
 *
 * @ProjectName: crossgate
 * @Package: com.aries.crossgate.modules.sys.controller
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 李贝留斯
 * @CreateDate: 2018/6/5 14:59
 * @Version: 1.0
 **/
@Controller
@RequestMapping("sys/online")
public class SysUserOnlineController {

    @Autowired
    private SysOnlineService sysOnlineService;

    @Log("进入在线用户列表页面")
    @GetMapping("/index.html")
    public String online() {
        return "base/online/onlineList";
    }

    @Log("查询在线用户列表数据")
    @ResponseBody
    @RequestMapping("/getOnlineUser.html")
    public JSONObject getOnlineUser(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<SysUserOnline> list= sysOnlineService.list();
        PageInfo pageInfo = new PageInfo(list);
        JSONObject jsonObject = JsonDataFormate.piecing(list,pageInfo);
        return jsonObject;
    }

    @Log("根据sessionId强踢用户下线")
    @ResponseBody
    @RequestMapping("/forceLogout/{sessionId}")
    public R forceLogout(@PathVariable("sessionId") String sessionId, RedirectAttributes redirectAttributes) {
        try {
            sysOnlineService.forceLogout(sessionId);
            return R.ok(Constant.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(Constant.ERROR);
        }
    }

    @Log("查询在线用户数据列表")
    @ResponseBody
    @RequestMapping("/sessionList")
    public Collection<Session> sessionList() {

        return sysOnlineService.sessionList();
    }


}
